package com.techsoul.module.order.controller;

import com.techsoul.common.core.pojo.Result;
import com.techsoul.module.order.service.IPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 支付 控制器
 *
 * @author liuen
 */
@Api(tags = "支付")
@RestController
@RequestMapping("/order/pay")
public class PayController {

    @Autowired
    private Result result;

    @Autowired
    private IPayService payService;

    @ApiOperation("微信小程序支付，根据小程序、小程序支付商户、微信用户，获取微信统一下单信息")
    @PreAuthorize("hasAnyAuthority('order:pay:insert')")
    @PostMapping("/wxAppletPay/{orderId}" )
    public Result wxAppletPay(@PathVariable("orderId") String orderId, HttpServletRequest request) {
        return result.success(payService.wxAppletPay(orderId, request));
    }

    @ApiOperation("微信支付成功回调方法，小程序调用")
    @PreAuthorize("hasAnyAuthority('order:pay:search')")
    @PostMapping("/wxAppletPaySuccess/{orderId}" )
    public Result wxAppletPaySuccess(@PathVariable("orderId") String orderId) {
        payService.wxAppletPaySuccess(orderId);
        return result.success();
    }

    @ApiOperation("微信服务器异步通知")
    @PreAuthorize("hasAnyAuthority('order:pay:search')")
    @RequestMapping(value = "/wxPayNotify", method = {RequestMethod.POST, RequestMethod.GET})
    public String wxPayNotify(HttpServletRequest request) {
        return payService.wxPayNotify(request);
    }

}
